﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NEWSPAPERINFOMATION
{
    public partial class Book : Form
    {
        String userid, bookpaperid;
        DataTable dt = new DataTable();
        string areaid;  //配送地区
        float bookprice;//报刊价格
        int require, issue;//订阅份数，订阅期刊数

        public Book(String bookpaper,String userid)
        {
            InitializeComponent();
            this.userid = userid;  //用户id 用于查找用户的姓名
            bookpaperid = bookpaper;  //报刊号 用于查找报刊名称

            this.textuser.Enabled = false;
            this.textnewspaper.Enabled = false;
            this.button1.Enabled = false;

            //下拉框选择配送省
            string sql = "SELECT * FROM newspaper.Province";
            BookConnection dbp = new BookConnection();
            dt = dbp.runSql(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string name = dt.Rows[i]["provincename"].ToString();
                    this.comboBox1.Items.Add(name);
                }
            }


            //查询并显示用户名称
            String sql1 = "SELECT * FROM users.Custom where customid='"+userid+"'";
            BookConnection db1 = new BookConnection();
            DataTable dt1 = new DataTable();
            dt1 = db1.runSql(sql1);
            this.textuser.Text = dt1.Rows[dt1.Rows.Count-1]["customname"].ToString();

            //查询并显示报刊名称
            String sql2 = "SELECT * FROM newspaper.Newspaper where newspaperid='" + bookpaperid + "'";
            dt1 = db1.runSql(sql2);
            this.textnewspaper.Text = dt1.Rows[dt1.Rows.Count - 1]["newspapername"].ToString();
            bookprice=float.Parse(dt1.Rows[dt1.Rows.Count - 1]["price"].ToString());

            //显示报刊单价
            this.textprice.Text="¥"+bookprice.ToString();
            this.textprice.Enabled = false;
           

        }

   

        private void button_inventory_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem == null)
            {
                return;
            }

            string inputarea = comboBox1.SelectedItem.ToString();
            
            String sql = "SELECT * FROM newspaper.Province WHERE provincename='" + inputarea + "'";
            BookConnection db2 = new BookConnection();
            dt = db2.runSql(sql);
            if(inputarea.Equals(""))
            {
                MessageBox.Show("配送省不能为空哦");
               
            }
            else  if (dt.Rows.Count == 0)
            {
                MessageBox.Show("您所输入的省暂时不在配送范围内哦！");
            }
            else
            {
                areaid=dt.Rows[dt.Rows.Count-1]["provinceid"].ToString();
                String sql2 = "SELECT * FROM newspaper.Inventory where provinceid='" + areaid + "' AND newspaperid='" + bookpaperid + "'";
                dt = db2.runSql(sql2);
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("对不起，该地区无货！\n请重新确认收货地");
                    this.button1.Enabled = false;
                }
                else
                {
                    MessageBox.Show("该地区有货！");
                    this.button1.Enabled = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
           
            if (this.textrequirenum.Text.Equals(""))
            {
                MessageBox.Show("订阅份数不能为空哦！");
                return;
            }
            if (this.textissuesum.Text.Equals(""))
            {
                MessageBox.Show("订阅期数不能为空哦！");
                return;
            }
            if ((this.textaddress.Text).Equals(""))
            {
                MessageBox.Show("投递详细地址不能为空哦！");
                return;
            }
            try
            {
                require = int.Parse(this.textrequirenum.Text);
                issue = int.Parse(this.textissuesum.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请输入正确订阅的数量！");
                return;
            }
            float money = require * issue * bookprice;
            if (MessageBox.Show("订单合计：¥ "+money.ToString()+"确定要订阅？", "请确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string saleid;
                if (DateTime.Now.Day<10)
                {
                    saleid = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "0" + DateTime.Now.Day.ToString();
                }
                else{
                    saleid = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                }

                //确认此订单是当日的第几个订单（流水号)
                string sql1 = "select * from sales.Booking where left(orderid,8)='"+saleid+"'";
                BookConnection db=new BookConnection();
                dt=db.runSql(sql1);
                if (dt.Rows.Count == 0)
                {
                    saleid = saleid + "0001";
                }
                else
                {
                    if (dt.Rows.Count < 9)
                    {
                        saleid = saleid + "000" + (dt.Rows.Count + 1).ToString();
                    }
                    else if (dt.Rows.Count < 99)
                    {
                        saleid = saleid + "00" + (dt.Rows.Count + 1).ToString();
                    }
                    else if (dt.Rows.Count < 999)
                    {
                        saleid = saleid + "0" + (dt.Rows.Count + 1).ToString();
                    }
                    else
                    {
                        saleid = saleid + (dt.Rows.Count + 1).ToString();
                    }
                }

                //插入
                
                string sql2="INSERT INTO sales.Booking VALUES('"+saleid+"','"+userid+"','"+bookpaperid+"','"+bookprice+"','"+areaid+"','"+this. comboBox1.SelectedItem.ToString()+"','"+require+"','"+issue+"','0','进行中','admin1')";
                int n = db.executeSqlResult(sql2);
                if (n > 0)
                {
                    MessageBox.Show("订购成功！！");
                }

            }
        }

       
     


      

      

       
    }
}
